/**
 * $Id: EditorKeyboardHandler.java,v 1.1 2009-10-23 11:32:08 gaudenz Exp $
 * Copyright (c) 2008, Gaudenz Alder
 */
package com.ht.impl.editor;

import com.mxgraph.swing.handler.mxKeyboardHandler;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.swing.util.mxGraphActions;
import com.mxgraph.view.mxGraph;

import javax.swing.*;
import java.awt.event.ActionEvent;

/**
 * @author Administrator
 */
public class EditorKeyboardHandler extends mxKeyboardHandler {
    private static final int MOVE_STEP_SMALL = 1;
    private static final int MOVE_STEP_LARGE = 5;

    /**
     * @param graphComponent control target  
     */
    public EditorKeyboardHandler(mxGraphComponent graphComponent) {
        super(graphComponent);
    }

    /**
     * Return JTree's input map.
     */
    protected InputMap getInputMap(int condition) {
        InputMap map = super.getInputMap(condition);

        if (condition == JComponent.WHEN_FOCUSED && map != null) {
            map.put(KeyStroke.getKeyStroke("control S"), "save");
            map.put(KeyStroke.getKeyStroke("control shift S"), "saveAs");
            map.put(KeyStroke.getKeyStroke("control N"), "new");
            map.put(KeyStroke.getKeyStroke("control O"), "open");

            map.put(KeyStroke.getKeyStroke("control Z"), "undo");
            map.put(KeyStroke.getKeyStroke("control Y"), "redo");
            map
                    .put(KeyStroke.getKeyStroke("control shift V"),
                            "selectVertices");
            map.put(KeyStroke.getKeyStroke("control shift E"), "selectEdges");

            //added by dongshufeng, 2010-07-13
            map.put(KeyStroke.getKeyStroke("shift UP"), "shift_up");
            map.put(KeyStroke.getKeyStroke("shift DOWN"), "shift_down");
            map.put(KeyStroke.getKeyStroke("shift RIGHT"), "shift_right");
            map.put(KeyStroke.getKeyStroke("shift LEFT"), "shift_left");

            map.put(KeyStroke.getKeyStroke("ctrl shift UP"), "ctrl_shift_up");
            map.put(KeyStroke.getKeyStroke("ctrl shift DOWN"), "ctrl_shift_down");
            map.put(KeyStroke.getKeyStroke("ctrl shift RIGHT"), "ctrl_shift_right");
            map.put(KeyStroke.getKeyStroke("ctrl shift LEFT"), "ctrl_shift_left");
        }

        return map;
    }

    /**
     * Return the mapping between JTree's input map and JGraph's actions.
     */
    protected ActionMap createActionMap() {
        ActionMap map = super.createActionMap();

        map.put("save", new EditorActions.SaveAction(false));
        map.put("saveAs", new EditorActions.SaveAction(true));
        map.put("new", new EditorActions.NewAction());
        map.put("open", new EditorActions.OpenAction());
        map.put("undo", new EditorActions.HistoryAction(true));
        map.put("redo", new EditorActions.HistoryAction(false));
        map.put("selectVertices", mxGraphActions.getSelectVerticesAction());
        map.put("selectEdges", mxGraphActions.getSelectEdgesAction());

        //added by dongshufeng, 2010-07-13
        map.put("shift_up", new MyMoveAction("shift_up"));
        map.put("shift_down", new MyMoveAction("shift_down"));
        map.put("shift_right", new MyMoveAction("shift_right"));
        map.put("shift_left", new MyMoveAction("shift_left"));

        map.put("ctrl_shift_up", new MyMoveAction("ctrl_shift_up"));
        map.put("ctrl_shift_down", new MyMoveAction("ctrl_shift_down"));
        map.put("ctrl_shift_right", new MyMoveAction("ctrl_shift_right"));
        map.put("ctrl_shift_left", new MyMoveAction("ctrl_shift_left"));
        return map;
    }

    //added by dongshufeng, 2010-07-13
    class MyMoveAction extends AbstractAction {
        public MyMoveAction(String name) {
            super(name);
        }

        public void actionPerformed(ActionEvent e) {
            Object source = e.getSource();
            if (source instanceof mxGraphComponent) {
                String name = getValue(Action.NAME).toString();
                mxGraphComponent graphComponent = (mxGraphComponent) source;
                mxGraph graph = graphComponent.getGraph();
                Object[] cells = graph.getMovableCells(graph.getSelectionCells());

                if (name.equalsIgnoreCase("shift_left"))
                    graphComponent.getGraph().moveCells(cells, -MOVE_STEP_SMALL, 0);
                else if (name.equalsIgnoreCase("shift_right"))
                    graphComponent.getGraph().moveCells(cells, MOVE_STEP_SMALL, 0);
                else if (name.equalsIgnoreCase("shift_up"))
                    graphComponent.getGraph().moveCells(cells, 0, -MOVE_STEP_SMALL);
                else if (name.equalsIgnoreCase("shift_down"))
                    graphComponent.getGraph().moveCells(cells, 0, MOVE_STEP_SMALL);
                else if (name.equalsIgnoreCase("ctrl_shift_left"))
                    graphComponent.getGraph().moveCells(cells, -MOVE_STEP_LARGE, 0);
                else if (name.equalsIgnoreCase("ctrl_shift_right"))
                    graphComponent.getGraph().moveCells(cells, MOVE_STEP_LARGE, 0);
                else if (name.equalsIgnoreCase("ctrl_shift_up"))
                    graphComponent.getGraph().moveCells(cells, 0, -MOVE_STEP_LARGE);
                else if (name.equalsIgnoreCase("ctrl_shift_down"))
                    graphComponent.getGraph().moveCells(cells, 0, MOVE_STEP_LARGE);
            }
        }
    }
}
